package example.Assignment1;

import jade.core.AID;
import jade.core.Agent;
import jade.core.behaviours.Behaviour;
import jade.lang.acl.ACLMessage;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;

public class InputAgent extends Agent {

    @Override
    protected void setup() {
        addBehaviour(new Behaviour() {
            private boolean finished = false;
            @Override
            public void action() {
                doWait(1000);

//                String filepath="winequality-white.csv";//注意filepath的内容；
                Object[] args = getArguments(); //获取argument
                String filepath = "";
                if (args != null && args.length > 0) {
                    filepath = (String) args[0];
                    ACLMessage acl = new ACLMessage(ACLMessage.INFORM);//发送通知
                    AID r = new AID(); //agent的id

                    try {
                        BufferedReader reader = new BufferedReader(new FileReader(filepath));//换成你的文件名
                        String[] line = reader.readLine().split(",");
                        if (line[line.length-1].equals("UNS")){
                            r.setLocalName("Rec_classify"); //设置接受agent的本地名
                        }
                        else {
                            r.setLocalName("Rec_cluster");
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    acl.addReceiver(r);// 添加到ACL消息中
                    acl.setSender(getAID());//设置发送者的本地名
                    acl.setContent(filepath);//设置内容
                    send(acl);
                    System.out.println("local name is:" + getLocalName());
                    System.out.println(getLocalName()+" send Message to Rec");
                    System.out.println("the content is : "+acl.getContent());
                    finished=true;
                    doDelete();
                }
                else {
                    System.out.println("No file");
                    doDelete();
                }
            }

            @Override
            public boolean done() {
                return finished;
            }
        });
    }
}








